package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.intellij.openapi.application.ApplicationManager;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;

/**
 * @author synhard
 * @description TODO
 * @date 2024/5/22 10:47
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ConfigFrame extends JPanel {
    private PreConfigDialog dialog;
    private ConfigPanel configPanel;
    public ConfigFrame() {
        if (StringUtils.isEmpty(GlobalConfig.token)) {
            showPreConfigDialog();
        } else {
            switchToConfigPanel();
        }
    }

    public void showPreConfigDialog() {
        this.dialog = new PreConfigDialog();
        // token 为空或者 token 失效，弹出输入 token 的对话框
        ApplicationManager.getApplication().invokeLater(() -> {
            if (this.dialog.showAndGet()) {
                switchToConfigPanel();
            }
        });
    }

    public void switchToConfigPanel() {
        this.removeAll(); // 清除当前面板上的组件
        this.configPanel = new ConfigPanel();
        this.add(this.configPanel);
        this.revalidate(); // 重新验证组件
        this.repaint(); // 重绘组件
    }
}
